package com.caigou.component;

import com.alibaba.fastjson.JSON;
import com.caigou.bean.cms.entity.ChannelData;
import com.caigou.bean.cms.entity.SurveyFormStatistics;
import com.caigou.config.RabbitConfig;
import com.caigou.service.ArticlePromoterService;
import com.caigou.service.ChannelDataService;
import com.caigou.service.SurveyStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Slf4j
@Component
public class RabbitReceiver {

    @Resource
    private HtmlBuildImageComponent htmlBuildImageComponent;
    @Resource
    private HtmlDetailBuilderComponent htmlDetailBuilderComponent;
    @Resource
    private HtmlHomePageBuilderComponent htmlHomePageBuilderComponent;

    @Resource
    private ChannelDataService channelDataService;
    @Resource
    private ArticlePromoterService articlePromoterService;
    @Resource
    private SurveyStatisticsService surveyStatisticsService;

    /**
     * 监听文章内文分享页消息
     */
    @RabbitHandler
    @RabbitListener(queuesToDeclare = @Queue(RabbitConfig.DEFAULT_ROUTING))
    public void handlerDefaultMessage(String message) {
        try {
            log.debug("receive from "+ RabbitConfig.DEFAULT_ROUTING+": "+message);
            Map map = JSON.parseObject(message, Map.class);
            htmlBuildImageComponent.buildImageByHtmlStr(map.get("articleId").toString(),map.get("articleContent").toString());
        }catch(Exception e) {
            log.error(e.getMessage(), e);
        }
    }


    /**
     * 监听作者修改消息，重新生成该作者的所有文章静态html文件
     */
    @RabbitHandler
    @RabbitListener(queuesToDeclare = @Queue(RabbitConfig.DIRECT_QUEUE_CMS_ARTICLEAUTHOR))
    public void handlerCmsArticleAuthorMessage(String message) {
        try {
            log.info("receive from "+ RabbitConfig.DIRECT_QUEUE_CMS_ARTICLEAUTHOR+": "+message);
            Map map = JSON.parseObject(message, Map.class);
            Integer promoterId = (Integer)map.get("promoterId");
            if(promoterId!=null){
                articlePromoterService.updateArticleDetailHtmlByPromoterId(promoterId);
            }
        }catch(Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 监听爬虫发送的静态页生成的MQ消息
     * @param message mq消息
     */
    @RabbitHandler
    @RabbitListener(queuesToDeclare = @Queue(RabbitConfig.DIRECT_QUEUE_CMS_BUILD_ARTICLE))
    public void handlerCmsBuildArticleMessage(Message message){
        try {
            log.info("receive from "+ RabbitConfig.DIRECT_QUEUE_CMS_BUILD_ARTICLE+": "+message);
            String messageBody = new String(message.getBody());
            log.info("解析的message为 "+messageBody);
            Map map = JSON.parseObject(messageBody, Map.class);
            ChannelData channelData = channelDataService.loadLastChannelData(map.get("articleId").toString());
            if (null == channelData) {
                htmlDetailBuilderComponent.buildArticleHtml(map.get("articleId").toString(), "999");
            } else {
                htmlDetailBuilderComponent.buildArticleHtml(map.get("articleId").toString(), channelData.getPlateId());
            }
        } catch (Exception e){
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 监听爬虫发送的频道页生成的MQ消息
     * @param message mq消息
     */
    @RabbitHandler
    @RabbitListener(queuesToDeclare = @Queue(RabbitConfig.DIRECT_QUEUE_CMS_BUILD_PAGE))
    public void handlerCmsBuildPageMessage(Message message){
        try {
            log.info("receive from "+ RabbitConfig.DIRECT_QUEUE_CMS_BUILD_PAGE+": "+message);
            String messageBody = new String(message.getBody());
            log.info("解析的message为 "+messageBody);
            Map map = JSON.parseObject(messageBody, Map.class);
            String url = htmlHomePageBuilderComponent.buildHomePageHtml(map.get("plateId").toString());
            log.info("分站消息重新生成静态页："+url);
        } catch (Exception e){
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 监听智能表单统计数据新增
     * @param message mq消息
     */
    @RabbitHandler
    @RabbitListener(queuesToDeclare = @Queue(RabbitConfig.FORM_SAVE_STATISTICS))
    public void handlerFormSaveStatisticsMessage(String message){
        try {
            log.info("receive from "+ RabbitConfig.FORM_SAVE_STATISTICS+": "+message);
            SurveyFormStatistics formStatistics = JSON.parseObject(message, SurveyFormStatistics.class);
            int result1 = surveyStatisticsService.saveSurveyFormStatistics(formStatistics);
            log.info("表单访问次数增加，result={}",result1);
        }catch(Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
